/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.github.pig.admin.controller;

import java.util.Date;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.github.pig.admin.service.SysZuulRouteService;
import com.github.pig.common.constant.CommonConstant;
import com.github.pig.common.entity.SysZuulRoute;
import com.github.pig.common.util.Query;
import com.github.pig.common.util.R;
import com.github.pig.common.web.BaseController;

/**
 * <p>
 * 动态路由配置表 前端控制器
 * </p>
 *
 * @author lengleng
 * @since 2018-05-15
 */
@RestController
@RequestMapping("/route")
public class ZuulRouteController extends BaseController {
	@Autowired
	private SysZuulRouteService sysZuulRouteService;

	/**
	 * 通过ID查询
	 *
	 * @param id
	 *            ID
	 * @return SysZuulRoute
	 */
	@GetMapping("/{id}")
	public SysZuulRoute get(@PathVariable Integer id) {
		return sysZuulRouteService.selectById(id);
	}

	/**
	 * 分页查询信息
	 *
	 * @param params
	 *            分页对象
	 * @return 分页对象
	 */
	@RequestMapping("/page")
	public Page page(@RequestParam Map<String, Object> params) {
		params.put(CommonConstant.DEL_FLAG, CommonConstant.STATUS_NORMAL);
		return sysZuulRouteService.selectPage(new Query<>(params), new EntityWrapper<>());
	}

	/**
	 * 添加
	 *
	 * @param sysZuulRoute
	 *            实体
	 * @return success/false
	 */
	@PostMapping
	public R<Boolean> add(@RequestBody SysZuulRoute sysZuulRoute) {
		return new R<>(sysZuulRouteService.insert(sysZuulRoute));
	}

	/**
	 * 删除
	 *
	 * @param id
	 *            ID
	 * @return success/false
	 */
	@DeleteMapping("/{id}")
	public R<Boolean> delete(@PathVariable Integer id) {
		SysZuulRoute sysZuulRoute = new SysZuulRoute();
		sysZuulRoute.setId(id);
		sysZuulRoute.setUpdateTime(new Date());
		sysZuulRoute.setDelFlag(CommonConstant.STATUS_DEL);
		return new R<>(sysZuulRouteService.updateById(sysZuulRoute));
	}

	/**
	 * 编辑
	 *
	 * @param sysZuulRoute
	 *            实体
	 * @return success/false
	 */
	@PutMapping
	public R<Boolean> edit(@RequestBody SysZuulRoute sysZuulRoute) {
		sysZuulRoute.setUpdateTime(new Date());
		return new R<>(sysZuulRouteService.updateById(sysZuulRoute));
	}

	/**
	 * 刷新配置
	 *
	 * @return success/fasle
	 */
	@GetMapping("/apply")
	public R<Boolean> apply() {
		return new R<>(sysZuulRouteService.applyZuulRoute());
	}
}
